<template>
  <div class="userMess" v-if="userId">
    <!-- vip等级 -->
    <div class="messLeft" v-if="showVip" @click="$router.push('/vip-rule')">
      <image-load filePath="defi/组 249.png" class="userMessImg"></image-load>
    </div>
    <svg-load name="userImg" class="userMessImg" v-else></svg-load>

    <div class="messRight">
      <div class="rightTop fw-bold">
        {{ maskString(address) }}
        <span class="vipclass">VIP {{ vipClass }}</span>
      </div>

      <Copy :data="userId" :contentFix="initial">
        <template #copyMsg>
          <!-- ✅ 这里用隐藏后的 userId -->
          <div class="rightTop fw-bold" style="font-size: 12px;">ID: {{ (userId) }}</div>
        </template>
      </Copy>

      <div class="rightBot" style="font-size: 12px;color: #000;font-weight: 650;">
        {{ _t18('credit_score') }}：{{ userStore.userInfo.detail?.credits }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { _t18 } from '@/utils/public'
import Copy from '@/components/common/Copy/index.vue'
import { useUserStore } from '@/store/user/index'
import { useMainStore } from '@/store/index'
import { computed, ref } from 'vue'

const userStore = useUserStore()
const mainStore = useMainStore()
const userId = computed(() => userStore.userInfo.user?.mdKey)
const address = computed(() => userStore.userInfo.user?.loginName)
const initial = ref('initial')
const showVip = ref(mainStore.getVIPClass?.isOpen)

const userAccount = computed(() => {
  const temp = userStore.userInfo.asset?.filter((item) => item.symbol === 'usdt' && item.type === 1)
  return temp?.[0]?.availableAmount || 0
})

const vipClass = computed(() => {
  let temp = 0
  let start = ''
  let end = ''
  for (const key in mainStore.getVIPClass) {
    if (userAccount.value > mainStore.getVIPClass[key]) start = key
    if (userAccount.value <= mainStore.getVIPClass[key]) {
      end = key
      break
    }
  }
  if (start.replace('Start', '') === end.replace('End', '')) {
    temp = start.replace('Start', '').replace('vip', '')
  }
  return temp
})

// ✅ 中间打码函数 — 只保留前2位和后2位
const maskString = (str) => {
  if (!str || str.length <= 4) return str
  return str.slice(0, 2) + '****' + str.slice(-2)
}
</script>

<style lang="scss" scoped>
.userMess {
  padding: 10px 10px;
  display: flex;
  align-items: center;
  .userMessImg {
    width: 50px;
    height: 50px;
    border-radius: 50%;
  }
  .messRight {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 5px 0;
    .rightTop {
      font-size: 16px;
      color: var(--ex-default-font-color);
    }
    .rightBot {
      font-size: 14px;
      color: var(--ex-passive-font-color);
    }
  }
}

.vipclass {
  color: #f5d658;
  margin-left: 10px;
}
.messLeft {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  p {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 18px;
    color: #333;
  }
}
</style>
